package com.comp437.uno.model;


import java.io.IOException;
import java.io.ObjectOutputStream;

import message.message;
import android.util.Log;

public class SocketWriter extends Thread {
	private String userID;
	private boolean finished=true;
	ObjectOutputStream ObjectOS = null;
	message writeMessage=null;
	private boolean first=true;;
	private Model model;
	SocketWriter(ObjectOutputStream ObjectOS){
		this.ObjectOS = ObjectOS;
		writeMessage=new message();
	}
	
	protected void setUserID(String userID){
		this.userID=userID;
	}
	
	/**
	 * Call this funcion if we need to Sign Up
	 * @param username
	 * @param password
	 * @param gender
	 */
	protected void setMessage(String username, String password,int flag){

		writeMessage = new message();
		writeMessage.setUsername(username);
		writeMessage.setPassword(password);
		writeMessage.setSender(username);
		writeMessage.setFlag(flag);
			Log.i("Chat",new Integer(writeMessage.flag).toString());
		this.run(writeMessage);
	}
	
	protected void setMessage(String username, String password){

		writeMessage = new message();
		writeMessage.setString(password);
		writeMessage.setSender(username);
		this.run(writeMessage);
	}
	
	protected void setMessage(int cardSize, String content){
	
		writeMessage = new message();
		writeMessage.setSize(cardSize);
		writeMessage.setString(content);
		writeMessage.setSender(model.getUserid());
		this.run(writeMessage);
	}
	
	protected void setMessage(int cardSize, String content,boolean token){

		writeMessage = new message();
		writeMessage.setSize(cardSize);
		writeMessage.setString(content);
		writeMessage.setSender(model.getUserid());
		writeMessage.setToken(token);
		this.run(writeMessage);
	}
	/**
	 * 
	 * @param content
	 * @param token
	 */
	protected void setMessage(String content, boolean token){

		writeMessage = new message();
		writeMessage.setString(content);
		writeMessage.setSender(model.getUserid());
		writeMessage.setToken(token);
		this.run(writeMessage);
	}
	/**
	 * Call this function if we need to send the "ASRL" or ....
	 * @param content
	 */
	protected void setMessage(String content){
			
		writeMessage = new message();
		writeMessage.setString(content);
		
		
		if(content.equals("AFRL")){
			writeMessage.setFlag(2);
			writeMessage.setSender(null);	
		}
		else if(content.length()==8){
		
			writeMessage.setFlag(4);
			writeMessage.setSender(content);	
		}
		
		this.run(writeMessage);
	}
	/**
	 * Call this function if the the Server need the USERID
	 */
	protected void setMessage(){

		writeMessage = new message();
		writeMessage.setSender(model.getUserid());
		this.run(writeMessage);
	}
	
	protected void setMessage(message message2){
		//writeMessage = message2;
		Log.i("Wirter","This is the Message: "+message2.getString());
		this.run(message2);
		
	}
	
	
  public void run(message writeMessage) {
	  	super.run();
	  	writeMessage.setString(writeMessage.getString().toUpperCase());
    	try {
    		if(!first){
    			Log.i("Chat",writeMessage.getString()+"&&&&&"+writeMessage.getFlag()+"&&&"+writeMessage.getSender()+"%%"+userID);
			ObjectOS.writeObject(writeMessage);

    		}
    		else
    		{
    			
    			Log.i("Chat",writeMessage.getString()+"&&&&&"+writeMessage.getFlag()+"&&&"+writeMessage.getSender()+"%%"+userID);
    			first=false;
    			ObjectOS.writeObject(writeMessage);

    		}
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        try {
            sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.interrupt();
    }
	 
 
}
